package org.asl;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

/**
 * For getting the finger gestures.
 */
public class FingerGestureActivity extends Activity {
    private static final int MENU_ASL_TO_SPEECH = 8;

    private static final int MENU_SPEECH_TO_ASL = 9;
    private static int selected_option = 0;


    /**
     * Invoked during init to give the Activity a chance to set up its Menu.
     * 
     * @param menu the Menu to which entries may be added
     * @return true
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        menu.add(0, MENU_ASL_TO_SPEECH, 0, R.string.asl_to_speech);
        menu.add(0, MENU_SPEECH_TO_ASL, 0, R.string.speech_to_asl);

        return true;
    }

    /**
     * Invoked when the user selects an item from the Menu.
     * 
     * @param item the Menu entry which was selected
     * @return true if the Menu item was legit (and we consumed it), false
     *         otherwise
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case MENU_ASL_TO_SPEECH:
                return true;
            case MENU_SPEECH_TO_ASL:
                return true;
        }

        return false;
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        
        //finger mapping: thumb -A, index -B, middle - C, ring - D, pinky - E
        Thread.setDefaultUncaughtExceptionHandler( new DefaultExceptionHandler());
        RelativeLayout linLayout = new RelativeLayout(this);
      
        Log.e("onCreate", "Creating image views");
        System.out.println("Creating image views");
        int basePosLeft = 50;        
        int basePosTop = 190;
        ImageView baseImageView = getImageView( R.drawable.palm, 151, 204, basePosLeft, basePosTop, 0 );
        baseImageView.setId(0);
        linLayout.addView(baseImageView);

        ImageView thumbView = getImageView( R.drawable.thumb_1, 200, 176, basePosLeft+20, basePosTop-18, 0);
        thumbView.setId(1);
        linLayout.addView(thumbView);

        ImageView fingerIndexView = getImageView( R.drawable.finger_1, 56, 200, basePosLeft+140, basePosTop-180, 20);
        fingerIndexView.setId(2);
        linLayout.addView(fingerIndexView);

        ImageView fingerMiddleView = getImageView( R.drawable.finger_1, 56, 200, basePosLeft+33, basePosTop-127, 0);
        fingerMiddleView.setId(3);
        linLayout.addView(fingerMiddleView);

        ImageView fingerRingView = getImageView( R.drawable.finger_1, 56, 200, basePosLeft-5, basePosTop-110, 0);
        fingerIndexView.setId(4);
        linLayout.addView(fingerRingView);

        ImageView fingerPinkyView = getImageView( R.drawable.finger_1, 56, 200, basePosLeft-25, basePosTop-90, 0);
        fingerPinkyView.setId(5);
        linLayout.addView(fingerPinkyView);
        
        // set LinearLayout as ContentView
        setContentView(linLayout);
    }

    public ImageView getImageView( int image_int, int sizeX, int sizeY, int posLeft, int posTop, int rotateAngle) {
        ImageView imageView = new ImageView(this);
        RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(sizeX, sizeY);
        layout.setMargins( posLeft, posTop, 0, 0);
        imageView.setLayoutParams(layout);
        
        if( rotateAngle == 0 ) {
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        } else {
            Matrix matrix = new Matrix();
            //matrix.postScale(scaleWidth, scaleHeight);
            matrix.postRotate( rotateAngle);
            imageView.setScaleType(ImageView.ScaleType.MATRIX);
            imageView.setImageMatrix( matrix);
        }

        //imageView.setPadding(0, 0, 0, 0);
        imageView.setImageResource(image_int);
        
        imageView.setOnTouchListener(new View.OnTouchListener() {
        	
            @Override
            public boolean onTouch(View v, MotionEvent event) {
              Log.d("onTouch","position= ["+ event.getX() +", "+ event.getY() + "]");
              ImageView iv = (ImageView)v;
              iv.setImageResource(R.drawable.thumb_4);
              int id = iv.getId();
              selected_option = id * 10; 
              Log.d("onTouch","selected_option = "+ selected_option);
            	  
              Drawable drawable = iv.getDrawable();
              Log.d("onTouch","drawable = "+ drawable);

              Bitmap bitmap = iv.getDrawingCache();
              Log.d("onTouch","bitmap = "+ bitmap);
              //int color = bitmap.getPixel(event.getX(), event.getY());
              //Log.d("onTouch","position= ["+ event.getX() +", "+ event.getY() + "]");

              openBodyGuestureActivity();
              return true;
            }
            
          });

        return imageView;
    }
   
    public void openBodyGuestureActivity() {
    	Intent myIntent = new Intent();
    	myIntent.setClassName("org.asl", "org.asl.BodyGestureActivity");
    	startActivity(myIntent);    	
    }

    protected void onPause() {
        super.onPause();
    }

    /**
     * Notification that something is about to happen, to give the Activity a
     * chance to save state.
     * 
     * @param outState a Bundle into which this Activity should save its state
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }
    
}

